package com.tnovoselec.android.common.io;

import java.util.LinkedHashMap;


public class LRUCache<K, V> extends LinkedHashMap<K, V> {

	private static final long serialVersionUID = -1370687066700730113L;

	private final int maxEntries;

	public LRUCache(int maxEntries) {
		super(maxEntries + 1, 1, true);
		this.maxEntries = maxEntries;
	}

	@Override
	protected boolean removeEldestEntry(Entry<K, V> eldest) {
		// After size exceeds max entries, this statement returns true and the oldest value will be removed.
		// Since this map is access oriented, the oldest value would be least recently used.
		return size() > maxEntries;
	}
}